package com.ytjj.quartz.service;

import com.ytjj.qmyx.mall.model.AnalysisStatistics;
import com.ytjj.qmyx.mall.model.Channel;
import com.ytjj.qmyx.mall.model.PathConfig;
import com.ytjj.quartz.dao.AnalysisStatisticsDao;
import com.ytjj.quartz.dao.ChannelDao;
import com.ytjj.quartz.dao.PathConfigDao;
import com.ytjj.quartz.dao.UsersAnalysisDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UsersAnalysisService {

    @Autowired
    private ChannelDao channelDao;

    @Autowired
    private PathConfigDao pathConfigDao;

    @Autowired
    private UsersAnalysisDao usersAnalysisDao;

    @Autowired
    private AnalysisStatisticsDao analysisStatisticsDao;

    public void updateUsersAnalysis() {
        List<Channel> channelList = channelDao.getChannelList();
        List<PathConfig> pathConfigList = pathConfigDao.getPathCodeList();
        for (Channel ch : channelList) {
            for (PathConfig pc : pathConfigList) {
                log.info("开始跑渠道:{} pageCode :{} ",ch.getChannel(),pc.getPathCode());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String createDate = sdf.format(new Date());
                applyUsersAnalysis(createDate,pc.getPathCode(),pc.getDescr(),ch.getChannel());
                log.info("完成 渠道:{} pageCode :{} ",ch.getChannel(),pc.getPathCode());
            }
        }
    }

    public void updateUsersAnalysisCheck() {
        List<Channel> channelList = channelDao.getChannelList();
        List<PathConfig> pathConfigList = pathConfigDao.getPathCodeList();
        for (Channel ch : channelList) {
            log.info("开始跑渠道:{}",ch.getChannel());
            for (PathConfig pc : pathConfigList) {
                log.info("开始跑渠道:{} pageCode :{} ",ch.getChannel(),pc.getPathCode());
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Date today = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24);
                String createDate = sdf.format(today);
                applyUsersAnalysis(createDate,pc.getPathCode(),pc.getDescr(),ch.getChannel());
                log.info("完成 渠道:{} pageCode :{} ",ch.getChannel(),pc.getPathCode());
            }
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void applyUsersAnalysis(String createDate,String pathCode,String descr,String channel){
        AnalysisStatistics analysisStatistics = new AnalysisStatistics();
        analysisStatistics.setPathCode(pathCode);
        analysisStatistics.setDescr(descr);
        analysisStatistics.setChannel(channel);
        analysisStatistics.setPv(usersAnalysisDao.getPv(pathCode, channel,createDate));
        analysisStatistics.setUv(usersAnalysisDao.getUv(pathCode, channel,createDate));
        analysisStatistics.setCreateTime(createDate);
        analysisStatistics.setUpdateTime(new Date());
        AnalysisStatistics anStatistics = analysisStatisticsDao.queryStatistics(pathCode, channel, createDate);
        if (anStatistics == null) {
            analysisStatisticsDao.insertStatistics(analysisStatistics);
        } else {
            analysisStatistics.setId(anStatistics.getId());
            analysisStatisticsDao.updateStatistics(analysisStatistics);
        }
    }


}
